package com.jufa_3.controller;

import com.jufa_3.common.R;
import com.jufa_3.dto.MonitoringInfoVO;
import com.jufa_3.dto.SelectInfo;
import com.jufa_3.entity.MonitoringInfo;
import com.jufa_3.entity.MonitoringTask;
import com.jufa_3.entity.User;
import com.jufa_3.mapper.MonitoringInfoMapper;
import com.jufa_3.service.ComboOrderService;
import com.jufa_3.service.MonitoringTaskService;
import com.jufa_3.service.UserService;
import com.jufa_3.task.Monitoring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;


@RestController
@RequestMapping("/monitoringTask")
public class MonitoringTaskController {

    @Autowired
    private MonitoringInfoMapper monitoringInfoMapper;

    @Autowired
    private MonitoringTaskService monitoringTaskService;

    @Autowired
    private Monitoring monitoring;

    @Autowired
    private UserService userService;

    @Autowired
    private ComboOrderService comboOrderService;

    public static String dataTypesCN[] = {"开庭公告", "执行公告", "失信公告", "法院公告", "曝光台", "裁判文书"};

    @GetMapping("/test")
    public R<String> test() {
        monitoring.sayHello();
        return R.success("ok");
    }

    @GetMapping("/updata")
    public R<String> updata(Integer id) {
        monitoringInfoMapper.update(new MonitoringInfo(id, "Y"), null);
        return R.success("ok");
    }

    @GetMapping("/addMonitoring")
    public R<String> addMonitoring(SelectInfo sif, Integer timelang) {
        sif.setKeyWord(sif.getName() + "&" + sif.getCardNum() + "&" + sif.getMobile());
        LocalDateTime dateTime = LocalDateTime.now();
        if (timelang == 0) {
            dateTime = dateTime.plusMonths(3);
        } else if (timelang == 1) {
            dateTime = dateTime.plusMonths(6);
        } else if (timelang == 2) {
            dateTime = dateTime.plusMonths(8);
        } else if (timelang == 3) {
            dateTime = dateTime.plusYears(1);
        } else if (timelang == 4) {
            dateTime = dateTime.plusYears(2);
        }
        // 套餐扣费添加收费记录
        // 添加到监控列表
        monitoringTaskService.save(new MonitoringTask(
                comboOrderService.debit(sif).toString(),
                sif.getOpenId(),
                "personal",
                userService.lambdaQuery().eq(User::getOpenId, sif.getOpenId()).one().getMobile(),
                sif.getKeyWord(),
                dateTime,
                LocalDateTime.now()));
        return R.success("ok");
    }

    @GetMapping("/getMonitoringList")
    public R<Object> getMonitoringList(MonitoringTask monitoringTask) {
        List<MonitoringTask> monitoringTaskList = monitoringTaskService.lambdaQuery()
                .eq(MonitoringTask::getOpenId, monitoringTask.getOpenId())
                .eq(MonitoringTask::getEnable, '1').list();
        if (monitoringTaskList.size() > 0) {
            for (MonitoringTask mt : monitoringTaskList) {
                if (mt.getEndDate().isBefore(LocalDateTime.now())) {
                    mt.setState("0"); // 已过期
                }
            }
            return R.success(monitoringTaskList);
        } else {
            return R.error(2004,"无监测信息");
        }
    }

    @GetMapping("/getMonitorInfo")
    public R<Object> getMonitorInfo(String openId, Integer time, Integer weidu) {
        LocalDateTime dateTime = LocalDateTime.now();
        if (time == 2) {
            dateTime = dateTime.minusDays(7);
        } else if (time == 3) {
            dateTime = dateTime.minusDays(30);
        } else if (time == 4) {
            dateTime = dateTime.minusYears(1);
        } else if (time == 1) {
            dateTime = dateTime.with(LocalTime.MIN);
        }
        String dataType = "";
        if (weidu != 0) {
            dataType = dataTypesCN[weidu - 1];
        }
        List<MonitoringInfoVO> mri = monitoringInfoMapper.getMonitorRecordInfo(
                openId, dateTime.toString(), dataType);
        if (mri.size() == 0) {
            return R.error(2004,"无监测信息");
        } else {
            return R.success(mri);
        }
    }

}
